﻿Imports System.Globalization

Partial Public Class frm_Transfer_Product
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not (Session("hdfgrid") Is Nothing) Then
                Dim Count As Integer
                Dim hdfgrid As String = Session("hdfgrid")
                'ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('" & hdfgrid & "');", True)
                Dim gridProRow As String = Session("gridProRow")
                Dim dtTruckScale As DataTable = Session("TruckScale" & gridProRow)
                If (dtTruckScale Is Nothing) Then
                    dtTruckScale = CreateDT()
                End If
                If Not (Session("gridTransferRowEdit") Is Nothing) Then
                    txtPackage.Text = String.Empty
                    Dim gridTran As GridView = CType(Session("gridTransfer"), GridView)
                    Dim rowIndex As Integer = CInt(Session("gridTransferRowEdit"))

                    Dim dinf As New CultureInfo("en-US")


                    For Count = 0 To hdfgrid.Length - 1
                        Select Case (hdfgrid.Substring(Count, 1))
                            Case "M"
                                rowMT.Visible = True
                            Case "P"
                                rowPackage.Visible = True
                                If (Not dtTruckScale.Columns.Contains("package")) Then
                                    dtTruckScale.Columns.Add("package", GetType(String))
                                End If
                                txtPackage.Text = dtTruckScale.Rows(rowIndex)("package").ToString()
                            Case "B"
                                rowBAG.Visible = True
                                If (Not dtTruckScale.Columns.Contains("bag")) Then
                                    dtTruckScale.Columns.Add("bag", GetType(String))
                                End If
                                txtBAG.Text = dtTruckScale.Rows(rowIndex)("bag").ToString()
                            Case "T"
                                rowTank.Visible = True
                                If (Not dtTruckScale.Columns.Contains("drum")) Then
                                    dtTruckScale.Columns.Add("drum", GetType(String))
                                End If
                                txtTank.Text = dtTruckScale.Rows(rowIndex)("drum").ToString()
                            Case "C"
                                rowPCS.Visible = True
                                If (Not dtTruckScale.Columns.Contains("pcs")) Then
                                    dtTruckScale.Columns.Add("pcs", GetType(String))
                                End If
                                txtPCS.Text = dtTruckScale.Rows(rowIndex)("pcs").ToString()
                            Case "L"
                                rowLiter.Visible = True
                                If (Not dtTruckScale.Columns.Contains("litter")) Then
                                    dtTruckScale.Columns.Add("litter", GetType(String))
                                End If
                                txtLITER.Text = dtTruckScale.Rows(rowIndex)("litter").ToString()
                            Case "R"
                                rowRoll.Visible = True
                                If (Not dtTruckScale.Columns.Contains("roll")) Then
                                    dtTruckScale.Columns.Add("roll", GetType(String))
                                End If
                                txtROLL.Text = dtTruckScale.Rows(rowIndex)("roll").ToString()
                        End Select
                    Next

                    Dim dateTimeIn As String = gridTran.Rows(rowIndex).Cells(0).Text
                    Dim date1 As Date = Date.ParseExact(dateTimeIn, "dd MMM yyyy", dinf)
                    txtDate.Text = date1.ToString("dd/MM/yyyy", dinf)
                    txtStart.Text = gridTran.Rows(rowIndex).Cells(1).Text

                    Dim dateTimeOut As String = gridTran.Rows(rowIndex).Cells(2).Text
                    Dim date2 As Date = Date.ParseExact(dateTimeOut, "dd MMM yyyy", dinf)
                    txtDateTo.Text = date2.ToString("dd/MM/yyyy", dinf)
                    txtEnd.Text = gridTran.Rows(rowIndex).Cells(3).Text

                    txtMT.Text = gridTran.Rows(rowIndex).Cells(6).Text

                    txtTruck6.Text = String.Empty
                    txtTruck810.Text = String.Empty
                    txtTrailer.Text = String.Empty
                    txtTrailer2.Text = String.Empty
                    Dim sessName As String = "truckTypeGroup" & gridProRow & rowIndex
                    Dim dtTruckTypeCount As DataTable = CType(Session(sessName), DataTable)
                    'Dim TruckType As String = dtTruckTypeCount.Rows(rowIndex)("TruckType").ToString()
                    For Each row As DataRow In dtTruckTypeCount.Rows
                        Dim truckType As String = row("TruckType").ToString()
                        If (truckType = "รถยนต์บรรทุกไม่เกิน 6 ล้อ") Then
                            txtTruck6.Text = row("cntType").ToString()
                        ElseIf (truckType = "รถยนต์บรรทุก 8 หรือ 10 ล้อ") Then
                            txtTruck810.Text = row("cntType").ToString()
                        ElseIf (truckType = "รถพ่วง") Then
                            txtTrailer.Text = row("cntType").ToString()
                        ElseIf (truckType = "รถพ่วงมากกว่า 2 axle") Then
                            txtTrailer2.Text = row("cntType").ToString()
                        End If
                    Next

                Else
                    For Count = 0 To hdfgrid.Length - 1
                        Select Case (hdfgrid.Substring(Count, 1))
                            Case "M"
                                rowMT.Visible = True
                            Case "P"
                                rowPackage.Visible = True
                                If (Not dtTruckScale.Columns.Contains("package")) Then
                                    dtTruckScale.Columns.Add("package", GetType(String))
                                End If
                            Case "B"
                                rowBAG.Visible = True
                                If (Not dtTruckScale.Columns.Contains("bag")) Then
                                    dtTruckScale.Columns.Add("bag", GetType(String))
                                End If
                            Case "T"
                                rowTank.Visible = True
                                If (Not dtTruckScale.Columns.Contains("drum")) Then
                                    dtTruckScale.Columns.Add("drum", GetType(String))
                                End If
                            Case "C"
                                rowPCS.Visible = True
                                If (Not dtTruckScale.Columns.Contains("pcs")) Then
                                    dtTruckScale.Columns.Add("pcs", GetType(String))
                                End If
                            Case "L"
                                rowLiter.Visible = True
                                If (Not dtTruckScale.Columns.Contains("litter")) Then
                                    dtTruckScale.Columns.Add("litter", GetType(String))
                                End If
                            Case "R"
                                rowRoll.Visible = True
                                If (Not dtTruckScale.Columns.Contains("roll")) Then
                                    dtTruckScale.Columns.Add("roll", GetType(String))
                                End If
                        End Select
                    Next
                End If

            End If
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If (txtDate.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ Date');", True)
            Exit Sub
        End If
        If (txtStart.Text.Trim() = "") Or (txtEnd.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ Time');", True)
            Exit Sub
        End If
        'If (txtTruck6.Text.Trim() = "") Then
        '    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุจำนวน รถยนต์บรรทุกไม่เกิน 6 ล้อ ');", True)
        '    Exit Sub
        'End If
        'If (txtTruck810.Text.Trim() = "") Then
        '    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุจำนวน รถยนต์บรรทุก 8 หรือ 10 ล้อ');", True)
        '    Exit Sub
        'End If
        'If (txtTrailer.Text.Trim() = "") Then
        '    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุจำนวน รถพ่วง');", True)
        '    Exit Sub
        'End If
        'If (txtTrailer2.Text.Trim() = "") Then
        '    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุจำนวน รถพ่วงมากกว่า 2 axle');", True)
        '    Exit Sub
        'End If
        Dim Count As Integer
        Dim hdfgrid As String = Session("hdfgrid")
        For Count = 0 To hdfgrid.Split("$")(0).Length - 1
            Select Case (hdfgrid.Substring(Count, 1))
                Case "M"
                    If (txtMT.Text.Trim() = "") Then
                        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ MT');", True)
                        Exit Sub
                    End If
                Case "P"
                    If (txtPackage.Text.Trim() = "") Then
                        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ Package');", True)
                        Exit Sub
                    End If
                Case "B"
                    If (txtBAG.Text.Trim() = "") Then
                        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ BAG');", True)
                        Exit Sub
                    End If
                Case "T"
                    If (txtTank.Text.Trim() = "") Then
                        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ DRUM');", True)
                        Exit Sub
                    End If
                Case "C"
                    If (txtPCS.Text.Trim() = "") Then
                        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ PCS');", True)
                        Exit Sub
                    End If
                Case "L"
                    If (txtLITER.Text.Trim() = "") Then
                        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ LITER');", True)
                        Exit Sub
                    End If
                Case "R"
                    If (txtROLL.Text.Trim() = "") Then
                        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ DRUM');", True)
                        Exit Sub
                    End If
            End Select
        Next

        Dim gridProRow As String = Session("gridProRow")
        Dim gridTran As GridView = CType(Session("gridTransfer"), GridView)
        Dim dtTruckScale As DataTable = Session("TruckScale" & gridProRow)
        
        If (dtTruckScale Is Nothing) Then
            dtTruckScale = CreateDT()
        End If

        If (Session("gridTransferRowEdit") IsNot Nothing) Then
            Dim sumTruck As Integer = 0
            If (txtTruck6.Text <> "") Then
                sumTruck += CInt(txtTruck6.Text)
            End If
            If (txtTruck810.Text <> "") Then
                sumTruck += CInt(txtTruck810.Text)
            End If
            If (txtTrailer.Text <> "") Then
                sumTruck += CInt(txtTrailer.Text)
            End If
            If (txtTrailer2.Text <> "") Then
                sumTruck += CInt(txtTrailer2.Text)
            End If
            Dim rowIndex As Integer = CInt(Session("gridTransferRowEdit"))
            'Dim s As String = dtTruckScale.Rows(rowIndex)("maxId").ToString()
            dtTruckScale.Rows(rowIndex)("minId") = txtDate.Text & " " & txtStart.Text
            dtTruckScale.Rows(rowIndex)("maxId") = txtDateTo.Text & " " & txtEnd.Text
            dtTruckScale.Rows(rowIndex)("productWeight") = txtMT.Text
            dtTruckScale.Rows(rowIndex)("countTruck") = sumTruck.ToString()

            If (dtTruckScale.Columns.Contains("package")) Then
                dtTruckScale.Rows(rowIndex)("package") = txtPackage.Text
            End If

            If (dtTruckScale.Columns.Contains("bag")) Then
                dtTruckScale.Rows(rowIndex)("bag") = txtBAG.Text
            End If
            If (dtTruckScale.Columns.Contains("drum")) Then
                dtTruckScale.Rows(rowIndex)("drum") = txtTank.Text
            End If
            If (dtTruckScale.Columns.Contains("pcs")) Then
                dtTruckScale.Rows(rowIndex)("pcs") = txtPCS.Text
            End If
            If (dtTruckScale.Columns.Contains("litter")) Then
                dtTruckScale.Rows(rowIndex)("litter") = txtLITER.Text
            End If
            If (dtTruckScale.Columns.Contains("roll")) Then
                dtTruckScale.Rows(rowIndex)("roll") = txtROLL.Text
            End If

            Session("TruckScale" & gridProRow) = dtTruckScale

            Dim sessName As String = "truckTypeGroup" & gridProRow
            Dim dtTruckTypeCount As DataTable = CType(Session(sessName), DataTable)

            Dim truckTypeRow() As DataRow = dtTruckTypeCount.Select("TruckType = 'รถยนต์บรรทุกไม่เกิน 6 ล้อ'")
            If (truckTypeRow.Length > 0) Then
                If (txtTruck6.Text <> "") Then
                    truckTypeRow(0)("cntType") = txtTruck6.Text
                End If
            End If
            Dim truckType1Row() As DataRow = dtTruckTypeCount.Select("TruckType = 'รถยนต์บรรทุก 8 หรือ 10 ล้อ'")
            If (truckType1Row.Length > 0) Then
                If (txtTruck810.Text <> "") Then
                    truckType1Row(0)("cntType") = txtTruck810.Text
                End If
            End If
            Dim truckType2Row() As DataRow = dtTruckTypeCount.Select("TruckType = 'รถพ่วง'")
            If (truckType2Row.Length > 0) Then
                If (txtTrailer.Text <> "") Then
                    truckType2Row(0)("cntType") = txtTrailer.Text
                End If
            End If
            Dim truckType3Row() As DataRow = dtTruckTypeCount.Select("TruckType = 'รถพ่วงมากกว่า 2 axle'")
            If (truckType3Row.Length > 0) Then
                If (txtTrailer2.Text <> "") Then
                    truckType3Row(0)("cntType") = txtTrailer2.Text
                End If
            End If


            Dim dtTruckTypeCountOneRow As DataTable = CType(Session(sessName & rowIndex), DataTable)

            Dim truckTypeOneRow() As DataRow = dtTruckTypeCountOneRow.Select("TruckType = 'รถยนต์บรรทุกไม่เกิน 6 ล้อ'")
            If (truckTypeOneRow.Length > 0) Then
                If (txtTruck6.Text <> "") Then
                    truckTypeOneRow(0)("cntType") = txtTruck6.Text
                End If
            End If
            Dim truckTypeOne1Row() As DataRow = dtTruckTypeCountOneRow.Select("TruckType = 'รถยนต์บรรทุก 8 หรือ 10 ล้อ'")
            If (truckTypeOne1Row.Length > 0) Then
                If (txtTruck810.Text <> "") Then
                    truckTypeOne1Row(0)("cntType") = txtTruck810.Text
                End If
            End If
            Dim truckTypeOne2Row() As DataRow = dtTruckTypeCountOneRow.Select("TruckType = 'รถพ่วง'")
            If (truckTypeOne2Row.Length > 0) Then
                If (txtTrailer.Text <> "") Then
                    truckTypeOne2Row(0)("cntType") = txtTrailer.Text
                End If
            End If
            Dim truckTypeOne3Row() As DataRow = dtTruckTypeCountOneRow.Select("TruckType = 'รถพ่วงมากกว่า 2 axle'")
            If (truckTypeOne3Row.Length > 0) Then
                If (txtTrailer2.Text <> "") Then
                    truckTypeOne3Row(0)("cntType") = txtTrailer2.Text
                End If
            End If


            Session(sessName) = dtTruckTypeCount
            Session(sessName & rowIndex) = dtTruckTypeCountOneRow
        Else
            Dim sessName As String = "truckTypeGroup" & gridProRow
            Dim dtTruckTypeCount As DataTable = CType(Session(sessName), DataTable)
            If (dtTruckTypeCount Is Nothing) Then
                dtTruckTypeCount = CreateDtTruckTypeCount()
            End If
            Dim sumTruck As Integer = 0
            If (txtTruck6.Text <> "") Then
                sumTruck += CInt(txtTruck6.Text)
            End If
            If (txtTruck810.Text <> "") Then
                sumTruck += CInt(txtTruck810.Text)
            End If
            If (txtTrailer.Text <> "") Then
                sumTruck += CInt(txtTrailer.Text)
            End If
            If (txtTrailer2.Text <> "") Then
                sumTruck += CInt(txtTrailer2.Text)
            End If
            Dim rowIndex As Integer = dtTruckScale.Rows.Count

            'Dim s As String = dtTruckScale.Rows(rowIndex)("maxId").ToString()
            Dim row As DataRow = dtTruckScale.NewRow()

            row("minId") = txtDate.Text & " " & txtStart.Text
            row("maxId") = txtDateTo.Text & " " & txtEnd.Text
            row("productWeight") = txtMT.Text
            row("countTruck") = sumTruck.ToString()

            dtTruckScale.Rows.Add(row)

            If (txtTruck6.Text <> "") Then
                Dim cRow As DataRow = dtTruckTypeCount.NewRow()
                cRow("TruckType") = "รถยนต์บรรทุกไม่เกิน 6 ล้อ"
                cRow("cntType") = txtTruck6.Text
                dtTruckTypeCount.Rows.Add(cRow)
            End If
            If (txtTruck810.Text <> "") Then
                Dim cRow As DataRow = dtTruckTypeCount.NewRow()
                cRow("TruckType") = "รถยนต์บรรทุก 8 หรือ 10 ล้อ"
                cRow("cntType") = txtTruck810.Text
                dtTruckTypeCount.Rows.Add(cRow)
            End If
            If (txtTrailer.Text <> "") Then
                Dim cRow As DataRow = dtTruckTypeCount.NewRow()
                cRow("TruckType") = "รถพ่วง"
                cRow("cntType") = txtTrailer.Text
                dtTruckTypeCount.Rows.Add(cRow)
            End If
            If (txtTrailer2.Text <> "") Then
                Dim cRow As DataRow = dtTruckTypeCount.NewRow()
                cRow("TruckType") = "รถพ่วงมากกว่า 2 axle"
                cRow("cntType") = txtTrailer2.Text
                dtTruckTypeCount.Rows.Add(cRow)
            End If

            Dim dt1 As DataTable = CType(Session("TruckScale" & gridProRow), DataTable)

            If (dt1 IsNot Nothing) Then
                dt1.Merge(dtTruckScale)
            Else
                dt1 = dtTruckScale
            End If

            Dim dt2 As DataTable = CType(Session(sessName), DataTable)
            If (dt2 IsNot Nothing) Then
                dt2.Merge(dtTruckTypeCount)
            Else
                dt2 = dtTruckTypeCount
            End If

            Session("TruckScale" & gridProRow) = dt1
            Session(sessName) = dt2
            Session(sessName & rowIndex) = dtTruckTypeCount
        End If

        'db.queryString += "MAX(TruckScaleID) maxId ,"
        'db.queryString += "MIN(TruckScaleID) minId ,"
        'db.queryString += "SUM(Product_Weight) as productWeight,"
        'db.queryString += "COUNT(TruckTypeID) as countTruck"

        'Dim dt As DataTable = CreateDT()
        'Dim dr As DataRow = dt.NewRow()
        'dr.Item("_Date") = txtDate.Text
        'dr.Item("_DateSort") = CDate(ClassFunctionGetData.CDateT(txtDate.Text)).ToString("yyyyMMdd")
        'dr.Item("_Time") = txtStart.Text
        'dr.Item("_Date2") = txtDateTo.Text
        'dr.Item("_Time2") = txtEnd.Text
        'dr.Item("_STruck") = CInt(txtTruck6.Text) + CInt(txtTruck810.Text) + CInt(txtTrailer.Text) + CInt(txtTrailer2.Text)
        'dr.Item("_Truck6") = txtTruck6.Text
        'dr.Item("_Truck810") = txtTruck810.Text
        'dr.Item("_Trailer") = txtTrailer.Text
        'dr.Item("_Trailer2") = txtTrailer2.Text
        'dr.Item("_M") = txtMT.Text
        'dr.Item("_P") = txtPackage.Text
        'dr.Item("_B") = txtBAG.Text
        'dr.Item("_T") = txtTank.Text
        'dr.Item("_C") = txtPCS.Text
        'dr.Item("_R") = txtROLL.Text
        'dr.Item("_L") = txtLITER.Text
        'dr.Item("_Remark") = txtRemark.Text
        'dt.Rows.Add(dr)
        'dt.AcceptChanges()
        'Session("TransferValue") = dt
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModalPopupTransfer('S');", True)
    End Sub
    Private Function CreateDtTruckTypeCount() As DataTable
        Dim dt As New DataTable()
        dt.Columns.Add("cntType", GetType(String))
        dt.Columns.Add("TruckType", GetType(String))
        Return dt
    End Function

    Private Function CreateDT() As DataTable
        Dim dt As New DataTable

        dt.Columns.Add("maxId", GetType(String))
        dt.Columns.Add("minId", GetType(String))
        dt.Columns.Add("productWeight", GetType(String))
        dt.Columns.Add("countTruck", GetType(String))

        'db.queryString += "MAX(TruckScaleID) maxId ,"
        'db.queryString += "MIN(TruckScaleID) minId ,"
        'db.queryString += "SUM(Product_Weight) as productWeight,"
        'db.queryString += "COUNT(TruckTypeID) as countTruck"

        'dt.Columns.Add("_Date", GetType(String))
        'dt.Columns.Add("_Time", GetType(String))
        'dt.Columns.Add("_Date2", GetType(String))
        'dt.Columns.Add("_Time2", GetType(String))
        'dt.Columns.Add("_DateSort", GetType(String))
        'dt.Columns.Add("_STruck", GetType(String))
        'dt.Columns.Add("_Truck6", GetType(String))
        'dt.Columns.Add("_Truck810", GetType(String))
        'dt.Columns.Add("_Trailer", GetType(String))
        'dt.Columns.Add("_Trailer2", GetType(String))
        'dt.Columns.Add("_M", GetType(String))
        'dt.Columns.Add("_P", GetType(String))
        'dt.Columns.Add("_B", GetType(String))
        'dt.Columns.Add("_T", GetType(String))
        'dt.Columns.Add("_C", GetType(String))
        'dt.Columns.Add("_R", GetType(String))
        'dt.Columns.Add("_L", GetType(String))
        'dt.Columns.Add("_Remark", GetType(String))
        'dt.AcceptChanges()
        Return dt
    End Function

    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Session("hdfgrid") = Nothing
        Session("IndexBulkProduct") = Nothing
        Session("ValueBulk") = Nothing
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModalPopupTransfer('N');", True)
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "")
        End If
    End Sub
End Class